import type { Bom, BomItem } from "@/pages/base/bom/entity";
import apiClient from "../../apiClient";

export interface PageListReq {
  //物料产品ID
  materialId?: number;
  //是否启用
  status?: string;
  //当前页码
  page: number;
  //每页数量
  pageSize: number;
}

export interface PageListRes {
  //总记录数
  total: number;
  //数据列表
  records: Bom[];
}

const pageList = (data: PageListReq): Promise<PageListRes> =>
  apiClient.post({
    url: "/base/bom/pageList",
    data
});
const getInfo = (id: React.Key): Promise<Bom> =>
  apiClient.get({
    url: `/base/bom/${id}`
});

const add = (data: Bom): Promise<Bom> =>
  apiClient.post({
    url: "/base/bom",
    data
});

const update = (data: Bom): Promise<Bom> =>
  apiClient.put({
    url: "/base/bom",
    data
});

const remove = (ids: React.Key[]): Promise<void> =>
  apiClient.delete({
    url: "/base/bom",
    params: {
    ids: ids.join(",")
  }
});

const updateStatus = (data: Bom) : Promise<void> =>
  apiClient.post({
    url: "/base/bom/status",
    data
  })

const updateDefault = (data: Bom) : Promise<void> =>
  apiClient.post({
    url: "/base/bom/default",
    data
  })

const getItems = (id: React.Key): Promise<BomItem[]> =>
  apiClient.post({
    url: "/base/bom/item/list",
    data: {
      bomId: id
    }
  });

// 获取产品BOM物料信息
const getBomByProduct = (productId: React.Key): Promise<BomItem[]> =>
  apiClient.get({
    url: `/base/bom/product/${productId}`,
  })  

export const bomService = {
  pageList,
  getInfo,
  add,
  update,
  remove,
  getItems,
  updateStatus,
  updateDefault,
  getBomByProduct
};

export default bomService;